Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Creating The Menu

One of the requirements for the IconStore application is the ability to dynamically build the Icons menu. To do this, we’ll need to query the ICONCATEGORY table and build the menu from the results. First, we need to read the database table and store the query results, as shown in Listing 8.4.

Listing 8.4 Reading the ICONCATEGORY table.

//————————————————————————————————————
// getCategories
// Read the IconStore CATEGORY table and create a Hashtable containing
// a list of all the categories. The key is the category description and
// the data value is the category ID.
//————————————————————————————————————
public Hashtable getCategories(
    Connection con)
{
    Hashtable table = new Hashtable();

    try {
        // Create a Statement object
        Statement stmt = con.createStatement();

        // Execute the query and process the results
        ResultSet rs = stmt.executeQuery(
                "SELECT DESCRIPTION,CATEGORY FROM ICONCATEGORY");

        // Loop while more rows exist
          while (rs.next()) {
              // Put the description and id in the Hashtable
                   table.put(rs.getString(1), rs.getString(2));
        }
        // Close the statement
          stmt.close();
    }
    catch (SQLException ex) {

        // An SQLException was generated. Dump the exception contents.
        // Note that there may be multiple SQLExceptions chained
        // together.

         System.out.println("\n*** SQLException caught ***\n");
         while (ex != null) {
                System.out.println("SQLState: " + ex.getSQLState());
               System.out.println("Message:  " + ex.getMessage());
            System.out.println("Vendor:   " + ex.getErrorCode());
          ex = ex.getNextException();
     }
          System.exit(1);
  }

    return table;
}

The flow of this routine is very basic, and we’ll be using it throughout our IconStore application. First, we create a Statement object; then, we submit an SQL statement to query the database; next, we process each of the resulting rows; and finally, we close the Statement. Note that a Hashtable object containing a list of all the categories is returned; the category description is the key and the category ID is the element. In this way, we can easily cross-reference a category description to an ID. We’ll see why this is necessary a bit later.

Now that all of the category information has been loaded, we can create our menu. Listing 8.5 shows how this is done.

Listing 8.5 Creating the Icons menu.

// Get a Hashtable containing an entry for each icon category.
// The key is the description and the data value is the
// category number.

categories = getCategories(connection);

// File menu
fileMenu = new Menu("File");
fileMenu.add(new MenuItem("Save As"));
fileMenu.add(new MenuItem("Exit"));
menuBar.add(fileMenu);

// Icons menu
sectionMenu = new Menu("Icons");

Enumeration e = categories.keys();
    int listNo = 0;
    String desc;

// Loop while there are more keys (category descriptions)
while (e.hasMoreElements()) {
    desc = (String) e.nextElement();

    // Add the description to the Icons menu
    sectionMenu.add(new MenuItem(desc));
}

// Add the Icons menu to the menu bar
menuBar.add(sectionMenu);

// Set the menu bar
setMenuBar(menuBar);

Notice that the Hashtable containing a list of the image categories is used to create our menu. The only way to examine the contents of a Hashtable without knowing each of the keys is to create an Enumeration object, which can be used to get the next key value of the Hashtable. Figure 8.1 shows our database-driven menu.


Figure 8.1  The IconStore menu.

Creating The Lists

Next on our agenda: creating the list boxes containing the image descriptions. We’ll create a list for each category, so when the user selects a category from the Icons menu, only a list of the images for the selected category will be shown. We’ll use a CardLayout to do this, which is a nifty way to set up any number of lists and switch between them effortlessly. For each of the categories that we read from the ICONCATEGORY table, we also read each of the image descriptions for that category from the ICONSTORE table and store those descriptions in a Hashtable for use later. At the same time, we add each description to a list for the category. Listing 8.6 shows the code used to read the ICONSTORE table.


Previous Table of Contents Next